<?php
	namespace  Home\Controller;
	class UserController extends CommonController{
		public function index(){
			$id=$_SESSION['home']['id'];
			$model=D('User');
			$list=$model->getuser($id);
			if($list['uff']!=='no.jpg'){
				$list['uff']='/Public/Uploads/'.$this->sub($list['uff']).'80_'.$list['uff'];
			}
			$this->assign('login',$_SESSION['home']);
			$this->assign('list',$list);
			$this->display();	
		}
		public function reset(){
			$name=$_SESSION['home']['name'];
			$id=$_SESSION['home']['id'];
			if(empty($_POST)){
				$this->assign('login',$_SESSION['home']);
				$this->assign('name',$name);
				$this->assign('id',$id);
				$this->display();
				return false;
			}
			$data['name']=$name;
			$data['pwd']=md5($_POST['pwd']);
			$data['id']=$id;
			$npwd=$_POST['npwd'];
			$model=D('User');
			$res=$model->rset($data,$npwd);
			if($res==1){
				unset($_SESSION['cart']);
				unset($_SESSION['home']);
				$this->success('修改成功',U('Index/login'),2);
			}elseif($res==0){
				$this->error('写入失败',U('index'));
	
			}elseif($res==3){
				$this->error('密码错误',U('index'));

			}
		}
		public function revise(){
			$id=$_SESSION['home']['id'];
			$model=D('User');
			$list=$model->getuser($id);
			$this->assign('login',$_SESSION['home']);
			$this->assign('list',$list);
			$this->display();
		}
		public function rwrite(){
			$data['name']=$_POST['name'];
			$data['sex']=$_POST['sex'];
			$data['age']=$_POST['age'];
			$data['tel']=$_POST['tel'];
			$data['birthday']=$_POST['birthday'];
			$data['email']=$_POST['email'];
			$data['qq']=$_POST['qq'];
			$file=$_POST['pic'];
			$filename=$this->upload();
			if(!$filename){
				$this->error('图片上传失败');
				return false;
			}
			// var_dump($filename);exit;
			$data['face']=$filename;
			$id=$_SESSION['home']['id'];
			$usermodel=D('User');
			$list=$usermodel->getuser($id);
			$dat=array_diff($data,$list);
			$da=array();
			foreach ($dat as $key => $value) {
				if($key=='sex'){
					$da['sex']=$value;
					unset($dat['sex']);
				}
			}
			if(!empty($da)){
				if($da['sex']=='男'){
					$da['sex']=1;
				}
				else{
					$da['sex']=0;
				}
				$res=$usermodel->where("id=".$id)->save($da);
				if(!$res){
					$this->error('性别修改失败');
				}
			}
			$infomodel=D('User_info');
			$resu=$infomodel->where("uid=".$id)->save($dat);
			if($resu){
				$this->success('修改成功',U('User/index'),2);

			}else{
				$this->error('修改失败');
			}
		}
		public function upload(){   
			 $upload = new \Think\Upload();
			// 实例化上传类    
			$upload->maxSize   =     3145728 ;
			// 设置附件上传大小    
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');


			//设置附件上传类型
			$upload->rootPath  =      './Public/'; 
			$upload->savePath  =      './Uploads/'; 
			$upload->saveName  =   date('Ymd',time()).uniqid();
			$upload->autoSub   = true;
			$upload->subName  =  date('Y',time()).'/'.date('m',time()).'/'.date('d',time());

			//设置附件上传目录
			//上传文件
			$info = $upload->upload();
			if(!$info){
				$this->error($upload->getError());
			}else{// 上传成功
				//做图片压缩方法
				$this->zoom($info['pic']['savename']);
				return $info['pic']['savename'];
			}
		}
		private function sub($str)
		{
			return substr($str,0,4).'/'.substr($str,4,2).'/'.substr($str,6,2).'/';
		}
		private function zoom($filename)
		{
			$image = new \Think\Image();
			$path='./Public/Uploads/'.$this->sub($filename);
			$image->open($path.$filename);
			$image->thumb(250,250)->save($path.'250_'.$filename);
			$image->thumb(150,150)->save($path.'150_'.$filename);
			$image->thumb(300,300)->save($path.'300_'.$filename);
			$image->thumb(80,80)->save($path.'80_'.$filename);
		}


		private function setunlink($filename)
		{
			$path='./Public/Uploads';
			unlink($path.$this->sub($filename).'150_'.$filename);
			unlink($path.$this->sub($filename).'250_'.$filename);
			unlink($path.$this->sub($filename).'300_'.$filename);
			unlink($path.$this->sub($filename).'80_'.$filename);
		}	

	}                             